package org.example.rubbish.mapper;


import org.apache.ibatis.annotations.*;
import org.example.rubbish.entity.Content;

import java.util.Date;
import java.util.List;

@Mapper
public interface ContentMapper {
    @Insert("INSERT INTO content(title, type, content, image_url, video_url, status, start_time, end_time, operator) " +
            "VALUES(#{title}, #{type}, #{content}, #{imageUrl}, #{videoUrl}, #{status}, #{startTime}, #{endTime}, #{operator})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Content content);

    @Update("UPDATE content SET title=#{title}, type=#{type}, content=#{content}, image_url=#{imageUrl}, " +
            "video_url=#{videoUrl}, status=#{status}, start_time=#{startTime}, end_time=#{endTime}, " +
            "operator=#{operator}, update_time=NOW() WHERE id=#{id}")
    void update(Content content);

    @Select("SELECT * FROM content WHERE id=#{id}")
    Content selectById(Long id);

    @Select("SELECT * FROM content WHERE status=1 AND (start_time IS NULL OR start_time <= NOW()) " +
            "AND (end_time IS NULL OR end_time >= NOW())")
    List<Content> selectActiveContents();

    @Select("SELECT * FROM content")
    List<Content> selectAll();
}
